1 Setup

suppressPackageStartupMessages({
  library(data.table)
  library(DESeq2)
  library(gplots)
  library(here)
  library(hyperSpec)
  library(parallel)
  library(pander)
  library(plotly)
  library(RColorBrewer)
  library(scatterplot3d)
  library(tidyverse)
  library(tximport)
  library(vsn)
})
source(here("UPSCb-common/src/R/featureSelection.R"))
pal <- brewer.pal(8,"Dark2")
hpal <- colorRampPalette(c("blue","white","red"))(100)
mar <- par("mar")
samples <- read_csv(here("doc/variables.csv"),
                      col_types=cols(
                        col_character(),
                        col_factor(),
                        col_factor(),
                        col_factor(),
                        col_factor()
                      ))

2 Raw data

tx2gene translation table

tx2gene <- suppressMessages(read_delim(here("reference/annotation/tx2gene.txt"),delim="\t",
                                       col_names=c("TXID","GENE")))

3 Raw data

filelist <- list.files(here("data/salmon"), 
                       recursive = TRUE, 
                       pattern = "quant.sf",
                       full.names = TRUE)

Sanity check to ensure that the data is sorted according to the sample info

names(filelist) <- sub("_S\\d+.*","",basename(dirname(filelist)))
stopifnot(all(samples$ID==names(filelist)))

Read the expression at the gene level

This gives us directly gene counts

counts <- suppressMessages(round(tximport(files = filelist, 
                                          type = "salmon",
                                          tx2gene=tx2gene)$counts))

This is for transcript counts

tx <- suppressMessages(tximport(files = filelist, 
                                      type = "salmon",
                                      txOut = TRUE))
tx.counts <- round(tx$counts)

# counts <- summarizeToGene(tx,tx2gene=tx2gene)

3.1 Quality Control

  • Check how many genes are never expressed
sel <- rowSums(tx.counts) == 0
sprintf("%s%% percent (%s) of %s genes are not expressed",
        round(sum(sel) * 100/ nrow(tx.counts),digits=1),
        sum(sel),
        nrow(tx.counts))
## [1] "14.9% percent (7940) of 53467 genes are not expressed"
  • Let us take a look at the sequencing depth, colouring by CHANGEME
dat <- tibble(x=colnames(tx.counts),y=colSums(tx.counts)) %>% 
  bind_cols(samples)

ggplot(dat,aes(x,y,fill=TISSUE)) + geom_col() + 
  scale_y_continuous(name="reads") +
  theme(axis.text.x=element_text(angle=90,size=4),axis.title.x=element_blank())

  • Display the per-gene mean expression

i.e. the mean raw count of every gene across samples is calculated and displayed on a log10 scale.

The cumulative gene coverage is as expected

ggplot(melt(log10(rowMeans(tx.counts))),aes(x=value)) + 
  geom_density() + ggtitle("gene mean raw tx.counts distribution") +
  scale_x_continuous(name="mean raw counts (log10)")
## Warning in melt(log10(rowMeans(tx.counts))): The melt generic in data.table
## has been passed a numeric and will attempt to redirect to the relevant reshape2
## method; please note that reshape2 is deprecated, and this redirection is now
## deprecated as well. To continue using melt methods from reshape2 while both
## libraries are attached, e.g. melt.list, you can prepend the namespace like
## reshape2::melt(log10(rowMeans(tx.counts))). In the next version, this warning
## will become an error.
## Warning: Removed 7940 rows containing non-finite values (stat_density).

The same is done for the individual samples colored by CHANGEME.

dat <- as.data.frame(log10(tx.counts)) %>% utils::stack() %>% 
  mutate(TISSUE=samples$TISSUE[match(ind,samples$ID)]) %>% 
  mutate(TIME=samples$TIME[match(ind,samples$ID)]) %>% 
  mutate(TREATMENT=samples$TREATMENT[match(ind,samples$ID)])

ggplot(dat,aes(x=values,group=ind,col=TISSUE)) + 
  geom_density() + ggtitle("sample raw counts distribution") +
  scale_x_continuous(name="per gene raw counts (log10)")
## Warning: Removed 661300 rows containing non-finite values (stat_density).

3.2 Export

dir.create(here("data/analysis/salmon"),showWarnings=FALSE,recursive=TRUE)
write.csv(tx.counts,file=here("data/analysis/salmon/raw-unormalised-gene-expression_data.csv"))

4 Data normalisation

4.1 Preparation

For visualization, the data is submitted to a variance stabilization transformation using DESeq2. The dispersion is estimated independently of the sample tissue and replicate.

dds <- DESeqDataSetFromMatrix(
  countData = tx.counts,
  colData = samples,
  design = ~ TISSUE * CONDITION)
## converting counts to integer mode

4.2 Remove bad sample

dds <- dds[,-match("P14065_119",colnames(dds))]

save(dds,file=here("data/analysis/salmon/dds.rda"))

Check the size factors (i.e. the sequencing library size effect)

dds <- estimateSizeFactors(dds)
sizes <- sizeFactors(dds)
pander(sizes)
Table continues below
P14065_101 P14065_102 P14065_103 P14065_104 P14065_105 P14065_106
0.9059 0.5975 0.7959 0.7663 1.074 0.9132
Table continues below
P14065_107 P14065_108 P14065_109 P14065_110 P14065_111 P14065_112
1.131 0.5848 0.7795 0.7331 0.4832 1.034
Table continues below
P14065_113 P14065_114 P14065_115 P14065_116 P14065_117 P14065_118
1.043 0.9788 0.6982 1.778 1.527 0.9935
Table continues below
P14065_120 P14065_121 P14065_122 P14065_123 P14065_124 P14065_125
1.158 1.857 0.9216 0.9973 1.959 1.693
P14065_126 P14065_127 P14065_128 P14065_129 P14065_130
1.079 1.081 1.135 1.317 1.151
boxplot(sizes, main="Sequencing libraries size factor")
abline(h=1,lty=2,col="grey")

4.3 Variance Stabilising Transformation

vsd <- varianceStabilizingTransformation(dds, blind=TRUE)
vst <- assay(vsd)
vst <- vst - min(vst)
  • Validation

The variance stabilisation worked adequately

meanSdPlot(vst[rowSums(vst)>0,])

4.4 QC on the normalised data

4.4.1 PCA

pc <- prcomp(t(vst))
percent <- round(summary(pc)$importance[2,]*100)
  • Cumulative components effect

We define the number of variable of the model

nvar=2

An the number of possible combinations

nlevel=nlevels(dds$TISSUE) * nlevels(dds$CONDITION)

We plot the percentage explained by the different components, the red line represent the number of variable in the model, the orange line the number of variable combinations.

ggplot(tibble(x=1:length(percent),y=cumsum(percent)),aes(x=x,y=y)) +
  geom_line() + scale_y_continuous("variance explained (%)",limits=c(0,100)) +
  scale_x_continuous("Principal component") + 
  geom_vline(xintercept=nvar,colour="red",linetype="dashed",size=0.5) + 
  geom_hline(yintercept=cumsum(percent)[nvar],colour="red",linetype="dashed",size=0.5) +
  geom_vline(xintercept=nlevel,colour="orange",linetype="dashed",size=0.5) + 
  geom_hline(yintercept=cumsum(percent)[nlevel],colour="orange",linetype="dashed",size=0.5)

4.4.2 3 first dimensions

The PCA shows that a large fraction of the variance is explained by both variables.

scatterplot3d(pc$x[,1],
              pc$x[,2],
              pc$x[,3],
              xlab=paste("Comp. 1 (",percent[1],"%)",sep=""),
              ylab=paste("Comp. 2 (",percent[2],"%)",sep=""),
              zlab=paste("Comp. 3 (",percent[3],"%)",sep=""),
              color=pal[as.integer(dds$CONDITION)],
              pch=c(17,19)[as.integer(dds$TISSUE)])
legend("topleft",
       fill=pal[1:nlevels(dds$CONDITION)],
       legend=levels(dds$CONDITION))

legend("topright",
       pch=c(17,19),
       legend=levels(dds$TISSUE))

par(mar=mar)

4.4.3 2D

pc.dat <- bind_cols(PC1=pc$x[,1],
                    PC2=pc$x[,2],
                    as.data.frame(colData(dds)))

The most variance comes from the tissues. In the leaf, the time has more effect than the treatment, while this look the opposite in the apex.

p <- ggplot(pc.dat,aes(x=PC1,y=PC2,col=CONDITION,shape=TISSUE,text=ID)) + 
  geom_point(size=2) + 
  ggtitle("Principal Component Analysis",subtitle="variance stabilized tx.counts")

ggplotly(p) %>% 
  layout(xaxis=list(title=paste("PC1 (",percent[1],"%)",sep="")),
         yaxis=list(title=paste("PC2 (",percent[2],"%)",sep="")))

4.4.4 Heatmap

Filter for noise

conds <- factor(paste(dds$TISSUE,dds$CONDITION))
sels <- rangeFeatureSelect(counts=vst,
                           conditions=conds,
                           nrep=2)

vst.cutoff <- 2
  • Heatmap of “all” genes
hm <- heatmap.2(t(scale(t(vst[sels[[vst.cutoff+1]],]))),
          distfun=pearson.dist,
          hclustfun=function(X){hclust(X,method="ward.D2")},
          labRow = NA,trace = "none",
          labCol = conds,
          col=hpal)

plot(as.hclust(hm$colDendrogram),xlab="",sub="")

4.5 Conclusion

The main differences in the leaf samples are according to the days of sampling. There are no obvious differences at day 3 between treatments but it looks different at day 1 with higher differences. In the case of the apex, the bigger differences appear at day 3. There is a clear separation at day 1 too but day 0 appear grouped close to at 1

5 Session Info

## R version 3.6.1 (2019-07-05)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.3 LTS
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
##  [1] grid      parallel  stats4    stats     graphics  grDevices utils    
##  [8] datasets  methods   base     
## 
## other attached packages:
##  [1] vsn_3.54.0                  tximport_1.14.0            
##  [3] forcats_0.4.0               stringr_1.4.0              
##  [5] dplyr_0.8.3                 purrr_0.3.3                
##  [7] readr_1.3.1                 tidyr_1.0.0                
##  [9] tibble_2.1.3                tidyverse_1.3.0            
## [11] scatterplot3d_0.3-41        RColorBrewer_1.1-2         
## [13] plotly_4.9.1                pander_0.6.3               
## [15] hyperSpec_0.99-20180627     ggplot2_3.2.1              
## [17] lattice_0.20-38             here_0.1                   
## [19] gplots_3.0.1.1              DESeq2_1.26.0              
## [21] SummarizedExperiment_1.16.0 DelayedArray_0.12.0        
## [23] BiocParallel_1.20.0         matrixStats_0.55.0         
## [25] Biobase_2.46.0              GenomicRanges_1.38.0       
## [27] GenomeInfoDb_1.22.0         IRanges_2.20.1             
## [29] S4Vectors_0.24.0            BiocGenerics_0.32.0        
## [31] data.table_1.12.6          
## 
## loaded via a namespace (and not attached):
##   [1] colorspace_1.4-1       rprojroot_1.3-2        htmlTable_1.13.2      
##   [4] XVector_0.26.0         base64enc_0.1-3        fs_1.3.1              
##   [7] rstudioapi_0.10        hexbin_1.28.0          farver_2.0.1          
##  [10] affyio_1.56.0          bit64_0.9-7            AnnotationDbi_1.48.0  
##  [13] lubridate_1.7.4        xml2_1.2.2             splines_3.6.1         
##  [16] geneplotter_1.64.0     knitr_1.26             zeallot_0.1.0         
##  [19] Formula_1.2-3          jsonlite_1.6           broom_0.5.2           
##  [22] annotate_1.64.0        cluster_2.1.0          dbplyr_1.4.2          
##  [25] shiny_1.4.0            BiocManager_1.30.10    compiler_3.6.1        
##  [28] httr_1.4.1             backports_1.1.5        fastmap_1.0.1         
##  [31] assertthat_0.2.1       Matrix_1.2-18          lazyeval_0.2.2        
##  [34] limma_3.42.0           cli_1.1.0              later_1.0.0           
##  [37] acepack_1.4.1          htmltools_0.4.0        tools_3.6.1           
##  [40] affy_1.64.0            gtable_0.3.0           glue_1.3.1            
##  [43] GenomeInfoDbData_1.2.2 reshape2_1.4.3         Rcpp_1.0.3            
##  [46] cellranger_1.1.0       vctrs_0.2.0            preprocessCore_1.48.0 
##  [49] gdata_2.18.0           nlme_3.1-142           crosstalk_1.0.0       
##  [52] xfun_0.11              rvest_0.3.5            testthat_2.3.0        
##  [55] mime_0.7               lifecycle_0.1.0        gtools_3.8.1          
##  [58] XML_3.98-1.20          zlibbioc_1.32.0        scales_1.1.0          
##  [61] promises_1.1.0         hms_0.5.2              yaml_2.2.0            
##  [64] memoise_1.1.0          gridExtra_2.3          rpart_4.1-15          
##  [67] latticeExtra_0.6-28    stringi_1.4.3          RSQLite_2.1.2         
##  [70] highr_0.8              genefilter_1.68.0      checkmate_1.9.4       
##  [73] caTools_1.17.1.2       rlang_0.4.2            pkgconfig_2.0.3       
##  [76] bitops_1.0-6           evaluate_0.14          labeling_0.3          
##  [79] htmlwidgets_1.5.1      bit_1.1-14             tidyselect_0.2.5      
##  [82] plyr_1.8.4             magrittr_1.5           R6_2.4.1              
##  [85] generics_0.0.2         Hmisc_4.3-0            DBI_1.0.0             
##  [88] pillar_1.4.2           haven_2.2.0            foreign_0.8-72        
##  [91] withr_2.1.2            survival_3.1-7         RCurl_1.95-4.12       
##  [94] nnet_7.3-12            modelr_0.1.5           crayon_1.3.4          
##  [97] KernSmooth_2.23-16     rmarkdown_1.18         locfit_1.5-9.1        
## [100] readxl_1.3.1           blob_1.2.0             reprex_0.3.0          
## [103] digest_0.6.23          xtable_1.8-4           httpuv_1.5.2          
## [106] munsell_0.5.0          viridisLite_0.3.0